{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4a26e9da",
   "metadata": {},
   "source": [
    "# 列表\n",
    "\n",
    "## 列表的生成\n",
    "\n",
    "用一对中括号`[]`生成列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9cea07cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1, 2.0, 'hello']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cce30d78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4ccb6f6",
   "metadata": {},
   "source": [
    "空列表的产生："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "26bdf4dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "empty_list = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a8473279",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "empty_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a192b644",
   "metadata": {},
   "source": [
    "空列表也可以这样产生："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8b259322",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cd29559",
   "metadata": {},
   "source": [
    "## 基本操作\n",
    "\n",
    "长度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bf205251",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9209b370",
   "metadata": {},
   "source": [
    "加法与数乘："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c92d2e10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2.0, 'hello', 1, 2.0, 'hello']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ac2da771",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2.0, 'hello', 1, 2.0, 'hello']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5116a1ff",
   "metadata": {},
   "source": [
    "索引和切片："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8a96d6d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [10, 11, 12, 13, 14]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f173f924",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "464dece8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ae347946",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 13]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2:-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63b9c465",
   "metadata": {},
   "source": [
    "用索引修改："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3cd5b861",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 11, 12, 13, 14]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1cc9c2e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "a[0] = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bec630d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[100, 11, 12, 13, 14]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c56663a9",
   "metadata": {},
   "source": [
    "用切片修改："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7b2da935",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [11, 12, 13, 14, 15]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "316f1fc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "a[1:3] = [1, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f700b2a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[11, 1, 2, 14, 15]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0e1910e",
   "metadata": {},
   "source": [
    "对于连续的切片（即步长为 1 ），Python采用的是整段替换的方法，两者的元素个数并不需要相同："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0b9a22e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "a[1:3] = [1, 2, 3, 4, 5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7defc225",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[11, 1, 2, 3, 4, 5, 14, 15]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb686d99",
   "metadata": {},
   "source": [
    "对于不连续（间隔step不为1）的片段进行修改时，两者的元素数目必须一致："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "34929fd6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 11, 2, 13, 3]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [10, 11, 12, 13, 14]\n",
    "a[::2] = [1, 2, 3]\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "431ee2f4",
   "metadata": {},
   "source": [
    "## 删除元素\n",
    "\n",
    "关键字 `del` 可以删除元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4a20fd4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1002, 'a', 'b', 'c']\n",
    "del a[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2b02c8de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d12aba53",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1002, 'a', 'b', 'c']\n",
    "del a[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a94e66ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1002]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3d6db4b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = ['a', 1, 'b', 2, 'c']\n",
    "del a[::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "773999cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efda2931",
   "metadata": {},
   "source": [
    "## 测试从属关系\n",
    "\n",
    "关键词`in`测试某个元素是否在列表中："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "badf3e23",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [10, 11, 12, 13, 14]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e13e2c78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 in a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "bfd48a9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 not in a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c9999b2",
   "metadata": {},
   "source": [
    "字符串也支持："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f4281439",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = 'hello world'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "93c6cd0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'he' in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "68e1786d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'world' not in s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4539f262",
   "metadata": {},
   "source": [
    "列表中可以包含各种对象，甚至可以包含列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8e55ca44",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [10, 'eleven', [12, 13]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "658aa289",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 13]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "893d8051",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2][1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75b20e7d",
   "metadata": {},
   "source": [
    "## 不改变自身的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2d76db3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [11, 12, 13, 11, 12]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7e565c3",
   "metadata": {},
   "source": [
    "`.count()`方法得到某个特定元素出现的次数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "c43e7b54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.count(11)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00521939",
   "metadata": {},
   "source": [
    "`.index()`方法得到某个元素第一次出现的索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "25aacdac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.index(11)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c280c482",
   "metadata": {},
   "source": [
    "## 改变自身的方法\n",
    "\n",
    "`.append()`方法添加单个元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7b53cec3",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [10, 11, 12]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "6e7a40cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.append(11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "4358008e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 11, 12, 11]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c9b0e79",
   "metadata": {},
   "source": [
    "只能添加为单个元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "ca5a1dd9",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.append([1, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2f7aeeb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 11, 12, 11, [1, 2]]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80fa359b",
   "metadata": {},
   "source": [
    "`.extend()`方法一次添加多个值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "0bf41a45",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [10, 11, 12]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "29c37ed9",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.extend([1, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "5fc91d15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 11, 12, 1, 2]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76f4c5a7",
   "metadata": {},
   "source": [
    "`.insert()`方法在指定索引位置处插入一个元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "5cedbed5",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [10, 11, 12, 13, 11]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "f08ea1bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.insert(2, 'a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "b6681e75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "4415c609",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 11, 'a', 12, 13, 11]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a2ac2bc",
   "metadata": {},
   "source": [
    "`.sort()`方法排序："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "807a1c28",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [10, 1, 11, 13, 11, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "4413009f",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "1b4f0747",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 10, 11, 11, 13]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "415d3413",
   "metadata": {},
   "source": [
    "`.reverse()`方法逆序："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "1324a5ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "c74dedc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[13, 11, 11, 10, 2, 1]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b3bc066",
   "metadata": {},
   "source": [
    "如果不想改变原来的列表，可以用函数`sorted()`和`reversed()`替代方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "31b760cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [10, 1, 11, 13, 11, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "02c02750",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 10, 11, 11, 13]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "7cc8eaf8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<list_reverseiterator at 0x10c6bf070>"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reversed(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afe5559e",
   "metadata": {},
   "source": [
    "`reversed()`返回的这个不是一个列表，可以使用`list()`函数转成列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "9964dbf3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 11, 13, 11, 1, 10]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(reversed(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "565e9a32",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
